package com.ruoyi.web.controller.trade;

import com.github.pagehelper.PageInfo;
import com.hundun.vision.api.dto.FeatureDTO;
import com.hundun.vision.api.dto.OperatorDTO;
import com.hundun.vision.api.dto.StrategyDTO;
import com.hundun.vision.api.query.StrategyQuery;
import com.hundun.vision.api.request.StrategyRequest;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.service.StrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：RuoChen
 * @date ：20:24 2021/3/2
 * @description：策略控制器
 */
@RestController
@RequestMapping("/trade/strategy")
public class StrategyController extends BaseController {
    @Autowired
    private StrategyService strategyService;

    @PreAuthorize("@ss.hasPermi('trade:strategy:save')")
    @Log(title = "策略管理", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody StrategyRequest request) {
        return toAjax(strategyService.save(request));
    }

    @PreAuthorize("@ss.hasPermi('trade:strategy:remove')")
    @Log(title = "策略管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{strategyIds}")
    public AjaxResult remove(@PathVariable List<Long> strategyIds) {
        return toAjax(strategyService.removeByIds(strategyIds));
    }

    @PreAuthorize("@ss.hasPermi('trade:strategy:update')")
    @Log(title = "策略管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody StrategyRequest request) {
        return AjaxResult.success(strategyService.update(request));
    }

    @PreAuthorize("@ss.hasPermi('trade:strategy:get')")
    @GetMapping(value = {"/", "/{strategyId}"})
    public AjaxResult get(@PathVariable(value = "strategyId", required = false) Long strategyId) {
        AjaxResult ajax = AjaxResult.success();
        List<FeatureDTO> features = strategyService.getFeatures();
        List<OperatorDTO> operators = strategyService.getOperators();
        ajax.put("features", features);
        ajax.put("operators", operators);
        if (StringUtils.isNotNull(strategyId)) {
            ajax.put(AjaxResult.DATA_TAG, strategyService.getById(strategyId));
        }
        return ajax;
    }

    @PreAuthorize("@ss.hasPermi('trade:strategy:search')")
    @GetMapping("/search")
    public TableDataInfo search(StrategyQuery query) {
        PageInfo<StrategyDTO> pageInfo = strategyService.search(query);
        return getDataTable(pageInfo);
    }
}
